package com.xingongchang.baolebei.protocol;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.external.activeandroid.Model;
import com.external.activeandroid.annotation.Column;
import com.external.activeandroid.annotation.Table;

@Table(name = "TOPIC_DETAIL")
public class TOPIC_DETAIL extends Model {

	@Column(name = "user_id")
	public int user_id;
	
	@Column(name = "isanonymous")
	public int isanonymous;

	@Column(name = "userphoto")
	public String userphoto;

	@Column(name = "replycount")
	public String replycount;

	@Column(name = "creaton")
	public String creaton;

	public String[] imgurl;

	@Column(name = "title")
	public String title;

	@Column(name = "body")
	public String body;

	@Column(name = "username")
	public String username;
	
	@Column(name = "imgId")
	public int[] imgId;

	public static TOPIC_DETAIL fromJson(JSONObject jsonObject)
			throws JSONException {
		if (null == jsonObject) {
			return null;
		}
		JSONArray subItemArray;
		TOPIC_DETAIL localItem = new TOPIC_DETAIL();

		localItem.isanonymous = jsonObject.optInt("isanonymous");
		localItem.user_id = jsonObject.optInt("user_id");
		localItem.title = jsonObject.optString("title");
		localItem.body = jsonObject.optString("body");
		localItem.replycount = jsonObject.optString("replycount");
		localItem.creaton = jsonObject.optString("creaton");

		subItemArray = jsonObject.optJSONArray("imgurl");
		if (null != subItemArray) {
			localItem.imgurl = new String[subItemArray.length()];
			localItem.imgId = new int[subItemArray.length()];
			for (int i = 0; i < subItemArray.length(); i++) {
				JSONObject subItem = subItemArray.getJSONObject(i);
				localItem.imgurl[i] = subItem.optString("filepath");
				localItem.imgId[i] =subItem.optInt("id");
			}
		}
		localItem.userphoto = jsonObject.optString("userphoto");
		localItem.username = jsonObject.optString("username");

		return localItem;
	}

}